import rtmplog
import time
import datetime

def mk_connect_msg(dat):
    m_head1 = [0x03, 0x00, 0x00, 0x00]
    m_head2 = [0x14, 0x00, 0x00, 0x00, 0x00]
    d_len = len(dat).to_bytes(3, byteorder='big')
    
    rdat = bytes(m_head1)+d_len+bytes(m_head2)
    return rdat
#生成窗口的大小设置 设置为
def mk_windowsize_msg():
    m_head1 = [ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x4b, 0x40]
    return bytes(m_head1)

#生成chunk的大小设置
def mk_chunksize_msg(chunksize):
    m_head1 = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00]
    v_val = int(chunksize).to_bytes(4, byteorder='big')
    rdat = bytes(m_head1)+v_val
    return rdat

#设置 Peer Bandwidth
def mk_bandwidth_msg():
    msg = [ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, \
            0x4b, 0x40, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
            0x00, 0x0f, 0xa0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x14, 0x00, 0x00, 0x00, 0x00, 0x02, \
            0x00, 0x07, 0x5f, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, \
            0x00, 0x00, 0x03, 0x00, 0x06, 0x66, 0x6d, 0x73, 0x56, 0x65, 0x72, 0x02, 0x00, 0x0d, 0x46, 0x4d, \
            0x53, 0x2f, 0x33, 0x2c, 0x30, 0x2c, 0x31, 0x2c, 0x31, 0x32, 0x33, 0x00, 0x0c, 0x63, 0x61, 0x70, \
            0x61, 0x62, 0x69, 0x6c, 0x69, 0x74, 0x69, 0x65, 0x73, 0x00, 0x40, 0x3f, 0x00, 0x00, 0x00, 0x00, \
            0x00, 0x00, 0x00, 0x00, 0x09, 0x03, 0x00, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x02, 0x00, 0x06, \
            0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x00, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x02, 0x00, 0x1d, 0x4e, \
            0x65, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6e, \
            0x6e, 0x65, 0x63, 0x74, 0x2e, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x00, 0x0b, 0x64, 0x65, \
            0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x02, 0x00, 0x15, 0x43, 0x6f, 0x6e, 0x6e, \
            0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x73, 0x75, 0x63, 0x63, 0x65, 0x65, 0x64, 0x65, 0x64, \
            0x2e, 0x00, 0x0e, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, \
            0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09 ]
    return bytes(msg)

def mk_result_msg():
    dat = [ 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0x5f, \
            0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, \
            0x00, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    return bytes(dat)

#生成onStatus的信息设置
def mk_onStatus_msg():

    rdat = [0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x14, 0x01, 0x00, 0x00, 0x00, \
                0x02, 0x00, 0x08, 0x6f, 0x6e, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, \
                0x00, 0x00, 0x00, 0x00, 0x05, 0x03, 0x00, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x02, 0x00, 0x06, \
                0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x00, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x02, 0x00, 0x17, 0x4e, \
                0x65, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x2e, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, \
                0x2e, 0x53, 0x74, 0x61, 0x72, 0x74, 0x00, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, \
                0x69, 0x6f, 0x6e, 0x02, 0x00, 0x10, 0x53, 0x74, 0x61, 0x72, 0x74, 0x20, 0x70, 0x75, 0x62, 0x6c, \
                0x69, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x09]


    return bytes(rdat)

def mk_onFCPublish_msg():

    dat = [0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x14, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x0b, 0x6f, \
            0x6e, 0x46, 0x43, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
            0x00, 0x00, 0x00, 0x05, 0x03, 0x00, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x02, 0x00, 0x06, 0x73, \
            0x74, 0x61, 0x74, 0x75, 0x73, 0x00, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x02, 0x00, 0x17, 0x4e, 0x65, \
            0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x2e, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x2e, \
            0x53, 0x74, 0x61, 0x72, 0x74, 0x00, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, \
            0x6f, 0x6e, 0x02, 0x00, 0x06, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x00, 0x00, 0x09]
    return bytes(dat)

def parase_AFM_Data(dat):
    #解析ADM格式的数据内容；
    #0x00:是数字类型 0x02：字符串 0x03:object类型 0x05：空只解析这几个类型的内容即可， 返回的是,因为只在command的情况下解析该数据内容，所以也不解析0x00的类型；
    r_str = []
    r_obj = {}

    while dat:
        dtype = dat[0]
        if dtype == 0x00:   #每个数字类型为占8个字节
            dat = dat[9:]
            continue
        elif dtype == 0x05:   #只占一个字节
            dat = dat[1:]
            continue
        elif dtype == 0x02:
            dlen = int.from_bytes(dat[1:3], byteorder='big', signed=False)
            t_val = dat[3:3+dlen]
            r_str.append(t_val)
            dat = dat[3+dlen:]
        
        elif dtype == 0x03:
            dat = dat[1:]
            while True:
                dlen = int.from_bytes(dat[:2], byteorder='big', signed=False)
                t_val = dat[2:2+dlen]
                dat = dat[2+dlen+1:]     #后续接的是值的类型，只提取str类型 ，所以忽略该值
                dlen = int.from_bytes(dat[:2], byteorder='big', signed=False)
                t_val1 = dat[2:2+dlen]
                dat = dat[2+dlen:]
                r_obj[t_val] = t_val1
                if not dat:
                    break

    return  r_str, r_obj

def parase_ChunkSize_Data(dat):
    #对于数据中的内容进行解析；chunksize 是4个字节；
    chunksize = int.from_bytes(dat, byteorder='big', signed=False)
    return chunksize

#获取typeid的消息名称
def prt_typeid_name(typeid):
    if typeid == 1:
        rtmplog.logger.info("Set Chunk Size 设置块的大小")
    if typeid == 2:
        rtmplog.logger.info("Abort Message 取消消息")
    if typeid == 3:
        rtmplog.logger.info("Acknowledgement 确认消息")
    if typeid == 4:
        rtmplog.logger.info("User Control Message 用户控制消息")
    if typeid == 5:
        rtmplog.logger.info("Window Acknowledgement Size 确认窗口大小")
    if typeid == 6:
        rtmplog.logger.info("Set Peer Bandwidth 设置对等端带宽")
    if typeid == 8:
        rtmplog.logger.info(" Audio message")
    if typeid == 9:
        rtmplog.logger.info("Video message")
    if typeid == 15 or typeid == 18:
        rtmplog.logger.info("Data message")
    if typeid == 16 or typeid == 19:
        rtmplog.logger.info("Shared object message")
    if typeid == 17 or typeid == 20:
        rtmplog.logger.info("Command message")

#时间戳转分钟和秒
def tm2hms(tm):
    timeStamp = int(tm / 1000)
    time_array = time.localtime(timeStamp)
    rstr = time.strftime("%H:%M:%S",time_array) 
    return rstr